import 'dart:async';
import 'package:efood_multivendor/controller/auth_controller.dart';
import 'package:efood_multivendor/controller/order_controller.dart';
import 'package:efood_multivendor/controller/splash_controller.dart';
import 'package:efood_multivendor/helper/responsive_helper.dart';
import 'package:efood_multivendor/helper/size_config.dart';
import 'package:efood_multivendor/util/dimensions.dart';
import 'package:efood_multivendor/view/base/custom_app_bar.dart';
import 'package:efood_multivendor/view/base/not_logged_in_screen.dart';
import 'package:efood_multivendor/view/screens/order/widget/order_view.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class OrderScreen extends StatefulWidget {
  @override
  _OrderScreenState createState() => _OrderScreenState();
}

class _OrderScreenState extends State<OrderScreen>
    with TickerProviderStateMixin {
  late TabController _tabController;
  bool _isLoggedIn = false;

  @override
  void initState() {
    super.initState();

    _isLoggedIn = Get.find<AuthController>().isLoggedIn();
    if (_isLoggedIn) {
      Get.find<SplashController>().setFirstOpen();
      _tabController = TabController(length: 2, initialIndex: 0, vsync: this);
      Get.find<OrderController>().getRunningOrders(1);
      Get.find<OrderController>().getHistoryOrders(1);
      Get.find<SplashController>().setFirstOpen();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: CustomAppBar(
          title: 'my_orders'.tr,
          isBackButtonExist: ResponsiveHelper.isDesktop(context)),
      body: _isLoggedIn
          ? RefreshIndicator(
              onRefresh: () async {
                if (_isLoggedIn) {
                  _tabController =
                      TabController(length: 2, initialIndex: 0, vsync: this);
                  Get.find<OrderController>().getRunningOrders(1);
                  Get.find<OrderController>().getHistoryOrders(1);
                }
              },
              child: SingleChildScrollView(
                child: GetBuilder<OrderController>(
                  builder: (orderController) {
                    return Column(children: [
                      Center(
                        child: Container(
                          width: Dimensions.WEB_MAX_WIDTH,
                          color: Colors.white,
                          height: 90.h,
                          child: OrderView(isRunning: true),
                        ),
                      ),
                    ]);
                  },
                ),
              ),
            )
          : NotLoggedInScreen(),
    );
  }
}
